package com.msb;

import java.awt.*;

/// 子弹类
public class Bullet {

    private int x;

    private int y;

    private final MyDir direction;

    public static final int width = ResourceMgr.BulletL.getWidth(), height = ResourceMgr.BulletL.getHeight();

    // 速率
    private static final int speed = 50;

    private final GameFrame frame;

    // 子弹是否存活
    private boolean live = true;

    // 分组
    private Group group = Group.Bad;

    // 矩形
    public Rectangle rect = new Rectangle(x, y, width, height);

    public Bullet(int x, int y, MyDir dir, GameFrame frame, Group group) {
        this.x = x;
        this.y = y;
        this.direction = dir;
        this.frame = frame;
        this.group = group;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    // 画子弹
    public void paint(Graphics g) {
        if (!live) {
            frame.bullets.remove(this);
        }
        Color color = g.getColor();
        g.setColor(Color.red);
//        g.fillRect(x, y, width, height);
        switch (direction) {
            case LEFT:
                g.drawImage(ResourceMgr.BulletL,x,y,null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.BulletR,x,y,null);
                break;
            case UP:
                g.drawImage(ResourceMgr.BulletU,x,y,null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.BulletD,x,y,null);
                break;
        }
        g.setColor(color);

        if (live) {
            move();
        }
    }

    // 子弹移动
    public void move() {
        switch (direction) {
            case UP:
                y -= speed;
                break;
            case DOWN:
                y += speed;
                break;
            case LEFT:
                x -= speed;
                break;
            case RIGHT:
                x += speed;
                break;
            default:
                break;
        }

        if (x > GameFrame.GAME_WIDTH || x < 0 || y > GameFrame.GAME_HEIGHT || y < 0) {
            live = false;
            frame.bullets.remove(this);
        } else {
            live = true;
        }

        rect.x = x;
        rect.y = y;
    }

    // 子弹和坦克碰撞
    public void collideWith(Tank tank) {

        // 子弹和坦克如果是同组的，不进行碰撞
        if (this.group == tank.getGroup()) return;

        // 子弹的范围
//        Rectangle rectangle1 = new Rectangle(x, y, width, height);
//
//        // 坦克的范围
//        Rectangle rectangle2 = new Rectangle(tank.getX(), tank.getY(), tank.width, tank.height);

        // 如果碰撞，子弹和坦克都消失
        if (rect.intersects(tank.rect)) {
            tank.die(); // 坦克死
            this.die(); // 子弹死


            int ex = tank.getX() + tank.getWidth() / 2 - Explode.WIDTH / 2;
            int ey = tank.getY() + tank.getHeight() / 2 - Explode.HEIGHT / 2;

            Explode explode = new Explode(ex,ey,frame);
            this.frame.explodes.add(explode);


        }
    }

    // 子弹死亡
    private void die() {
        live = false;
    }
}
